﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace SimpComm
{
    #region 用于传递消息的类
    /// <summary>
    /// 用于传递消息的类
    /// </summary>
    [DataContract]
    public class MsgBox
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        [DataMember]
        public bool Success { get; set; }

        /// <summary>
        /// 返回消息
        /// </summary>
        [DataMember]
        public string Message { get; set; }

        /// <summary>
        /// 状态标识(或ID)
        /// </summary>
        [DataMember]
        public int Status { get; set; }

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public MsgBox() { }

        /// <summary>
        /// 含参构造函数
        /// </summary>
        /// <param name="success">是否成功</param>
        /// <param name="message">返回消息</param>
        public MsgBox(bool success, string message = "") : this(success, message, 0) { }

        /// <summary>
        /// 含参构造函数
        /// </summary>
        /// <param name="success">是否成功</param>
        /// <param name="message">返回消息</param>
        /// <param name="status">状态标识(或ID)</param>
        public MsgBox(bool success, string message, int status)
        {
            Success = success;
            Message = message;
            Status = status;
        }

        public string ToJsonStr()
        {
            return JsonHelper.JsonSerializer(this);
        }
    }

    /// <summary>
    /// 用于传递消息的类
    /// </summary>
    /// <typeparam name="T">如果需要返回数据,则指定返回的数据类型,不需要返回数据,请使用非泛型类.</typeparam>
    public class MsgBox<T> : MsgBox
    {
        /// <summary>
        /// 传递的类
        /// </summary>
        public T Result { get; set; }

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public MsgBox() { }

        /// <summary>
        /// 构造函数
        /// </summary>
        public MsgBox(bool success, string msg) : this(success, msg, 0, default(T)) { }
        /// <summary>
        /// 构造函数
        /// </summary>
        public MsgBox(bool success, string msg, int status, T result)
            : base(success, msg, status)
        { Result = result; }
    }

    #endregion

    #region 单条查询返回类

    /// <summary>
    /// 单条查询返回类型 (蓝伟航 2012年8月13日)
    /// </summary>
    [DataContract]
    public class DataInfo<T>
    {
        private bool _Success = true; //初始为true

        /// <summary>
        /// 是否成功
        /// </summary>
        [DataMember]
        public bool Success
        {
            get { return _Success; }
            set { _Success = value; }
        }

        /// <summary>
        /// 返回消息
        /// </summary>
        [DataMember]
        public string Message { get; set; }

        /// <summary>
        /// 记录
        /// </summary>
        [DataMember]
        public T Data { get; set; }

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public DataInfo()
        {
        }
        public DataInfo(bool _Success, string _Message, T _Data)
        {
            Success = _Success;
            Message = _Message;
            Data = _Data;
        }
    }

    // 示例:
    //     DataInfo<ENT_INFO_FORM, ENT_EXT_INFO> data = (
    //                        from a in db.ENT_INFO_FORM
    //                        join b in db.ENT_EXT_INFO
    //                        on a.Ent_SN equals b.Ent_SN
    //                        select new DataInfo<ENT_INFO_FORM, ENT_EXT_INFO> { Data = a, Data2 = b }
    //                    ).Take(1).First();
    /// <summary>
    /// 单条查询返回类型___2个表
    /// </summary>
    public class DataInfo<T1, T2> : DataInfo<T1>
        where T1 : class
        where T2 : class
    {
        public DataInfo() { }

        public T2 Data2 { get; set; }
    }

    /// <summary>
    /// 单条查询返回类型___3个表
    /// </summary>
    public class DataInfo<T1, T2, T3> : DataInfo<T1, T2>
        where T1 : class
        where T2 : class
        where T3 : class
    {
        public DataInfo() { }

        public T3 Data3 { get; set; }
    }
    #endregion

    #region 分页查询返回类
    /// <summary>
    /// 分页查询返回类型 (蓝伟航 2012年8月10日)
    /// </summary>
    public class MD<T>
    {
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public MD()
        {
            Datas = new List<T>();
            MyRoutes = new Dictionary<string, object>();
        }

        /// <summary>
        /// 记录集总数
        /// </summary>
        public int PageIndex { get; set; }
        /// <summary>
        /// 记录集总数
        /// </summary>
        public int Count { get; set; }
        /// <summary>
        /// 每页数量
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 记录集
        /// </summary>
        public List<T> Datas { get; set; }

        /// <summary>
        /// 路由参数
        /// </summary>
        public Dictionary<string, object> MyRoutes { get; set; }


        /// <summary>
        /// 返回简单的分页类
        /// </summary>
        /// <returns></returns>
        public PagedModel GetPageMode()
        {
            return new PagedModel()
                       {
                           CurrentPageIndex = PageIndex,
                           PageSize = PageSize,
                           TotalRecordCount = Count,
                           MyRoutes = MyRoutes,
                       };
        }

        public SelectBoxView GetDefaultValue(string p)
        {
            foreach (var myRoute in MyRoutes)
            {
                if (myRoute.Key == p)
                {
                    return new SelectBoxView(myRoute.Value.ToString(), null);
                }
            }
            return null;
        }
    }
    /// <summary>
    /// 分页查询返回类型,添加返回额外的信息
    /// </summary>
    /// <typeparam name="T1"></typeparam>
    /// <typeparam name="T2"></typeparam>
    public class MD<T1, T2> : MD<T1>
        where T1 : class
        where T2 : class,new()
    {
        /// <summary>
        /// 初始函数
        /// </summary>
        public MD()
            : base()
        {
            AddiInfo = new T2();
        }
        /// <summary>
        /// 附加新信息的字段(类)
        /// </summary>
        public T2 AddiInfo { get; set; }
    }
    #endregion

    #region 多条件查询分页
    /// <summary>
    /// 多条件查询分页
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="Model"></typeparam>
    public class SMP<T, Model>
    {
        public Func<T, bool> selected = null;
        public Func<T, Model> getmodel = null;
        public DynamicOrderby dynamicOrderby = null;
        public int page = 1;
        public int pageSize = 15;
    }

    #endregion

    #region 多集合返回集

    public class MS<T1>
    {
        public T1 D1 { get; set; }
        public MS() { }
        public MS(T1 d1)
        {
            D1 = d1;
        }
    }

    public class MS<T1, T2> : MS<T1>
    {
        public T2 D2 { get; set; }
        public MS() { }
        public MS(T1 d1, T2 d2)
            : base(d1)
        {
            D2 = d2;
        }
    }

    public class MS<T1, T2, T3> : MS<T1, T2>
    {
        public T3 D3 { get; set; }
        public MS() { }
        public MS(T1 d1, T2 d2, T3 d3)
            : base(d1, d2)
        {
            D3 = d3;
        }
    }

    public class MS<T1, T2, T3, T4> : MS<T1, T2, T3>
    {
        public T4 D4 { get; set; }
        public MS() { }
        public MS(T1 d1, T2 d2, T3 d3, T4 d4)
            : base(d1, d2, d3)
        {
            D4 = d4;
        }
    }

    #endregion

}
